/**
 *查询时段报表与搜索框的模糊查询，有模糊查询，传日期名称字段
 */
/**查询全部的流量传感器 **/
var deviceId="",Grid="",Store="",columns="",store="",grid="",form="";
$.ajax({
        type: "GET",
        url: "/rest/web/device/selectDeviceList",
        dataType: "json",
        success: function (data) {
            var dtuList = data.data,
                dtuHtml = "";
            $.each(dtuList, function (i, o) {
                dtuHtml += '<option value="' + o.deviceId + '">' + o.name + '</option>'
            });
            $(".selectDevice").html(dtuHtml);
            $(".selectDevice").val(data.data[0].deviceId);
            deviceId=data.data[0].deviceId;
            query();
        }
    });
function query(){
        Grid = BUI.Grid;
        Store = BUI.Data.Store;
        columns = [
            {
                title: '序号', dataIndex: 'flowDailyReportId',width:'11%',renderer: function (value,obj,index) {
                    return index+1;
                }
            },
            {title: '设备名称', dataIndex: 'deviceName',width:'11%'},
            {title: '流量', dataIndex: 'flow',width:'11%', renderer: function (value) {

                    return value==""||value==null? "____ " : (value.toFixed(2)+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');

                }},
            {title: '流速', dataIndex: 'speed',width:'11%', renderer: function (value) {

                    return value==""||value==null? "____ " :(value.toFixed(2)+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');

                }},
            {title: '正向总量', dataIndex: 'totalPositive',width:'11%', renderer: function (value) {

                    return value==""||value==null? "____ " :(parseInt(value)+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');

                }},
            {title: '反向总量', dataIndex: 'totalNegative',width:'11%', renderer: function (value) {

                    return value==""||value==null? "____ " :(value.toFixed(2)+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');

                }},
            {title: '报警', dataIndex: 'alarm',width:'11%', renderer: function (value) {

                    return value==""||value==null? "____ " :(value.toFixed(2)+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');

                }},
            {title: '时间', dataIndex: 'createTime',width:'11%', renderer: function (value) {
                    return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
                }
            },
            {title: '备注', dataIndex: 'remark',width:'12%'}
        ];
//分页查询
    store = new Store({
            url: '/rest/web/history/selectFlowHistoryPage',
            autoLoad: true, //自动加载数据
            params: { //配置初始请求的参数
                length: '15',
                start: '0',
                deviceId:deviceId,
                startTime: new Date(new Date()).Format1("yyyy-MM-dd hh:mm:ss"),
                endTime:new Date(new Date()).Format("yyyy-MM-dd hh:mm:ss")
            },
            pageSize: 15,	// 配置分页数目
            root: 'data.list',
            totalProperty: 'data.totalCount'
        });
        grid = new Grid.Grid({
            render: '#grid',
            columns: columns,
            loadMask: true, //加载数据时显示屏蔽层
            store: store,
            plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
            // 底部工具栏
            bbar: {
                pagingBar: true
            }
        });

    grid.render();

    /**模糊搜索**/
    form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
}

//点击导出按钮，导出数据
var startTime="",endTime="",deviceId="";
function exportData(){
    startTime=$("input[name='startTime']").val();
    endTime=$("input[name='endTime']").val();
    deviceId=$(".selectDevice").val();
    console.log(startTime);
    console.log(endTime);
    console.log(deviceId);
    window.location.href = "/rest/web/history/excelHistoryDownload?startTime="+startTime+"&endTime="+endTime+"&deviceId="+deviceId;
}
